<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户注册页面</title>
    <link rel="stylesheet" href="lib/bootstrap.min.css">
    <style>
        .box {
            width: 1000px;
            margin: auto;
        }
        
        .wrap {
            width: 600px;
            height: 250px;
            background: cornsilk;
            margin: auto;
            padding: 20px 50px 20px 30px;
        }
        
        #register {
            margin-left: 20px;
        }
        
        .code {
            margin-left: 40px;
        }
    </style>
</head>

<body>
    <div class="box">
        <h2>用户注册信息</h2>
        <hr>
        <div class="form-horizontal wrap">
            <div class="form-group">
                <label for="username" class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="username" placeholder="请输入用户名">
                </div>
            </div>

            <div class="form-group">
                <label for="reallyName" class="col-sm-2 control-label">真实姓名</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="reallyName" placeholder="请输入真实姓名">
                </div>
            </div>

            <div class="form-group">
                <label for="password" class="col-sm-2 control-label">密码</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="password" placeholder="请输入密码">
                </div>
            </div>

            <div class="form-group">
                <label class="radio-inline code">
                        <input type="radio" name="type"  id="student" checked>学生
                    </label>
                <label class="radio-inline">
                        <input type="radio" name="type" id="teacher">老师
                    </label>
            </div>

            <button type="submit" class="btn btn-primary" id="register">注册</button>
        </div>
    </div>

    <script src="lib/jquery.min.js"></script>
    <script src="lib/common.js"></script>

    <script>
        //数据存入数据库请求
        $("#register").click(function() {
            let username = $("#username").val();
            let password = $("#password").val();
            let reallyName = $("#reallyName").val();
            if (username && password && reallyName) {
                $.ajax({
                    url: `${address}/register`,
                    method: "post",
                    data: {
                        username,
                        password,
                        reallyName,
                        usertype: $("#student")[0].checked ? 0 : 1
                    },
                    success(data) {
                        if (data === "error") {
                            alert("注册失败")
                        } else {
                            alert("注册成功")
                            location.href = `${address}/login.html`;
                        }
                    }

                })
            }
        })
    </script>
</body>

</html>